﻿#include "ImageWidget.h"
#include <QPainter>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QtMath>

ImageWidget::ImageWidget(QWidget* parent) : QLabel(parent)
{
	setAlignment(Qt::AlignCenter);
	setMouseTracking(true);  // 启用鼠标跟踪
}

bool ImageWidget::loadImage(const QString& path)
{
	if (originalImage.load(path)) {
		resetZoom();
		return true;
	}
	return false;
}

void ImageWidget::zoomIn()
{
	scaleFactor *= (1.0 + zoomStep);
	updateDisplayImage();
}

void ImageWidget::zoomOut()
{
	scaleFactor = qMax(0.1, scaleFactor * (1.0 - zoomStep));
	updateDisplayImage();
}

void ImageWidget::resetZoom()
{
	scaleFactor = 0.4;
	displayImage = originalImage;
	setPixmap(QPixmap::fromImage(displayImage));
	updateDisplayImage();
}

void ImageWidget::updateDisplayImage()
{
	if (!originalImage.isNull()) {
		// 使用平滑变换保持图片质量
		displayImage = originalImage.scaled(
			originalImage.size() * scaleFactor,
			Qt::KeepAspectRatio,
			Qt::FastTransformation
		);
		// 创建临时图像用于绘制
		QImage tempImage(size(), QImage::Format_RGB32);
		tempImage.fill(QColor("#333"));

		QPainter painter(&tempImage);
		QRect imgRect = QRect(
			(width() - displayImage.width()) / 2 + m_dragOffset.x(),
			(height() - displayImage.height()) / 2 + m_dragOffset.y(),
			displayImage.width(),
			displayImage.height()
		);

		painter.drawImage(imgRect, displayImage);

		setPixmap(QPixmap::fromImage(tempImage));
	}
}

void ImageWidget::wheelEvent(QWheelEvent* event)
{
	if (event->angleDelta().y() > 0) {
		zoomIn();
	} else {
		zoomOut();
	}
	event->accept();
}

QPointF ImageWidget::mapToImage(const QPoint& pos)
{
	if (originalImage.isNull()) return QPointF(-1, -1);
	// 计算图片显示区域
	QRect imgRect = QRect(
		(width() - displayImage.width()) / 2 + m_dragOffset.x(),
		(height() - displayImage.height()) / 2 + m_dragOffset.y(),
		displayImage.width(),
		displayImage.height()
	);

	// 检查是否在图片区域内
	if (!imgRect.contains(pos)) {
		return QPointF(-1, -1);
	}

	// 转换为图片坐标
	double x = (pos.x() - imgRect.x()) * originalImage.width() / static_cast<double>(imgRect.width());
	double y = (pos.y() - imgRect.y()) * originalImage.height() / static_cast<double>(imgRect.height());

	return QPointF(x, y);
}

void ImageWidget::mouseMoveEvent(QMouseEvent* event)
{
	if (m_dragging) {
		QPoint delta = event->pos() - m_lastDragPos;
		m_dragOffset += delta;
		m_lastDragPos = event->pos();
		updateDisplayImage();
	} else {
		QPointF imagePos = mapToImage(event->pos());

		if (imagePos.x() >= 0 && imagePos.y() >= 0 &&
			imagePos.x() < originalImage.width() &&
			imagePos.y() < originalImage.height())
		{
			QString info = QString("坐标: (%1, %2) | 原始尺寸: %3x%4 | 缩放: %5%")
				.arg(qRound(imagePos.x()))
				.arg(qRound(imagePos.y()))
				.arg(originalImage.width())
				.arg(originalImage.height())
				.arg(qRound(scaleFactor * 100));
			emit pixelPositionChanged(info);
		} else {
			emit pixelPositionChanged("鼠标在图片外");
		}
	}

	QLabel::mouseMoveEvent(event);
}

void ImageWidget::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton && !displayImage.isNull()) {
		m_dragging = true;
		m_lastDragPos = event->pos();
		setCursor(Qt::ClosedHandCursor);
	}
	QLabel::mousePressEvent(event);
}
void ImageWidget::mouseReleaseEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton) {
		m_dragging = false;
		setCursor(Qt::ArrowCursor);
	}
	QLabel::mouseReleaseEvent(event);
}

void ImageWidget::paintEvent(QPaintEvent* event)
{
	QLabel::paintEvent(event);

	// 添加缩放比例提示
	if (!originalImage.isNull()) {
		QPainter painter(this);
		painter.setPen(Qt::red);
		painter.drawText(10, 20, QString("缩放: %1%").arg(qRound(scaleFactor * 100)));
	}
}
